﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Card.Web.Member
{
    public partial class Cart : MemberBase
    {
        #region
        private DataTable m_dtCart;
        #endregion

        #region
        protected override void OnInit(EventArgs e)
        {
            this.rptList.ItemDataBound += new RepeaterItemEventHandler(rptList_ItemDataBound);
            this.rptList.ItemCommand += new RepeaterCommandEventHandler(rptList_ItemCommand);
            base.OnInit(e);
        }

        void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (string.Compare(e.CommandName, "DELETE", true) == 0)
            {
                int cartID = 0;
                if (int.TryParse(e.CommandArgument.ToString(), out cartID))
                {
                    Entity.Cart cart = BLL.CartManager.GetEntity(cartID, this.Passport);
                    if (cart != null)
                    {
                        Entity.OrderCard orderCard = BLL.OrderCardManager.GetEntity(cart.OrderCardID, this.Passport);
                        if (orderCard != null && string.IsNullOrEmpty(orderCard.OrderIds))
                        {
                            BLL.OrderCardManager.Delete(orderCard.ID, this.Passport);
                            BLL.OrderCardFieldManager.DeleteByOrderCardID(orderCard.ID, this.Passport);
                            BLL.OrderCardLogoManager.DeleteByOrderCardID(orderCard.ID, this.Passport);
                            Card.Common.CardWebUtility.DeleteOrderCardFiles(orderCard.ID,Passport);
                        }
                        BLL.CartManager.Delete(cart.ID, this.Passport);
                        Common.CardWebUtility.SetCartInfo(this.Passport.UserID);
                    } 
                }
                Response.Redirect("Cart.aspx");
            }
        }

        void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Footer)
            {
                Literal ltlTotalMoney = e.Item.FindControl("ltlTotalMoney") as Literal;
                if (ltlTotalMoney != null && this.m_dtCart != null && this.m_dtCart.Rows.Count > 0)
                {
                    ltlTotalMoney.Text = this.m_dtCart.Compute("SUM(TOTAL_MONEY)", string.Empty).ToString();
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.DoInit();
        }
        #endregion

        #region
        private void DoInit()
        {
            this.m_dtCart = BLL.CartManager.GetListByUserID(this.Passport.UserID);
            if (this.m_dtCart != null && this.m_dtCart.Rows.Count > 0)
            {
                this.rptList.DataSource = this.m_dtCart;
                this.rptList.DataBind();
            }
            else
            {
                this.tbEmpty.Visible = true;
                this.rptList.Visible = false;
            }
        }
        #endregion
    }
}
